package com.infore.exchange.main.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.infore.common.base.EppPage;
import com.infore.common.base.Msg;
import com.infore.common.base.ResultUtil;
import com.infore.common.base.WrapperHelp;
import com.infore.common.enums.ScheduleEnum;
import com.infore.common.exception.MyException;
import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.exchange.TJobInfo;
import com.infore.common.model.entity.exchange.TJobTaskInfo;
import com.infore.common.model.entity.exchange.TTaskInfo;
import com.infore.common.model.entity.vo.exchange.TJobInfoVo;
import com.infore.common.response.ApiEntityResponse;
import com.infore.common.response.ApiListResponse;
import com.infore.common.utils.UUIDGenerator;
import com.infore.exchange.main.quartz.CronTaskRegistrar;
import com.infore.exchange.main.quartz.SchedulingRunnable;
import com.infore.exchange.main.service.TJobInfoService;
import com.infore.exchange.main.service.TJobTaskInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;


/**
 * <p>
 * 作业信息表 前端控制器
 * </p>
 *
 * @author lijh
 * @since 2023-02-13
 */
@RestController
@Api(value = "作业信息表前端控制器", tags = "作业信息表前端控制器")
@RequestMapping("/tJobInfo")
public class TJobInfoController extends BaseController {
	private String prefix = "/tJobInfo";

	@Autowired
	private TJobInfoService TJobInfoService;

	@Autowired
	private TJobTaskInfoService TJobTaskInfoService;

	@Autowired
	private CronTaskRegistrar cronTaskRegistrar;

	@GetMapping()
	public String TJobInfoController(){
		return prefix;
	}

	/**
	 * 查询作业信息表列表
	 * @param tjobinfoVo 作业信息表的vo类
	 * @return Msg<Object> 结果集
	 * @author lijh
	 * @since 2023-02-13
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value = "查询作业信息表列表", notes = "作业信息表列表查询 ")
	public Msg<Object> list(TJobInfoVo tjobinfoVo){
		//sql查询条件
		Wrapper<TJobInfo> wrapper = null ;
		try {
			wrapper = (Wrapper<TJobInfo>) WrapperHelp.addCondition(new EntityWrapper<TJobInfo>(), tjobinfoVo);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error(500,e.getMessage());
		}
		//查询方法
		Page<TJobInfo> eppPage = new EppPage<TJobInfo>(TJobInfoService,tjobinfoVo,wrapper);

		eppPage = TJobInfoService.selectPage(eppPage, wrapper);
		for (TJobInfo job: eppPage.getRecords()) {
			List<TTaskInfo> tasks = TJobTaskInfoService.queryTaskInfoByJobId(job.getJobId(),null);
			job.setTasks(tasks);
		}
		return ResultUtil.successPage(eppPage);
	}

	@ResponseBody
	@ApiOperation(value = "添加或编辑作业信息表", notes = "添加或编辑作业信息表")
	@RequestMapping(value="/addOrEdit",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public Msg<Object> addOrEdit(@RequestBody TJobInfo tjobinfo){
		return TJobInfoService.addOrEdit(tjobinfo);
	}
	/**
	 * 删除作业信息表
	 *
	 * @param  jobId
	 * @return Msg<Object>
	 * @author lijh
	 * @since 2023-02-13
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "删除作业信息表", notes = "删除作业信息表")
	public Msg<Object> del(String jobId) {
		TJobInfo jobInfo = TJobInfoService.selectById(jobId);
		boolean flag = TJobInfoService.deleteJob(jobId);
		if(flag){
			SchedulingRunnable task = new SchedulingRunnable(jobInfo.getBeanName(), jobInfo.getMethodName(), jobInfo.getJobId());
			if(cronTaskRegistrar.hasCronTask(task)){
				cronTaskRegistrar.removeCronTask(task);
			}
		}
		return ResultUtil.setResult(flag);
	}



	@ApiOperation(value = "解析cron表达式", notes = "解析corn表达式，生成指定日期的时间序列")
	@ApiImplicitParam(name = "cronSchedule", value = "cron表达式", required = true, dataType = "String", paramType = "query")
	@RequestMapping(value = "/cronParser", method = RequestMethod.GET)
	public ApiListResponse cronParser(@RequestParam String cronSchedule) {
		List<String> result =new ArrayList<>();
		try {
			result = TJobInfoService.cronParser(cronSchedule);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new MyException("500","无法生成Cron表达式",e.getMessage());
		}
		return  ApiListResponse.ok(result);
	}
}
